package com.tfswue.cxy.controller;

import com.tfswue.cxy.entity.BorrowRecord;
import com.tfswue.cxy.service.BorrowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/borrow")
public class BorrowController {
    @Autowired
    private BorrowService borrowService;

    // 借书
    @PostMapping
    public String borrow(@RequestBody Map<String, Long> req) {
        Long userId = req.get("userId");
        Long bookId = req.get("bookId");
        boolean success = borrowService.borrowBook(userId, bookId);
        return success ? "success" : "book not found";
    }

    // 查询所有借阅记录
    @GetMapping
    public List<BorrowRecord> getAllRecords() {
        return borrowService.getAllRecords();
    }

    // 查询单条借阅记录
    @GetMapping("/{id}")
    public BorrowRecord getRecordById(@PathVariable Long id) {
        return borrowService.getRecordById(id);
    }

    // 删除借阅记录
    @DeleteMapping("/{id}")
    public String deleteRecord(@PathVariable Long id) {
        borrowService.deleteRecord(id);
        return "success";
    }

    // 修改借阅记录
    @PutMapping
    public String updateRecord(@RequestBody BorrowRecord record) {
        borrowService.updateRecord(record);
        return "success";
    }
}